#include "StateManager.h"

#include "Ogre.h"

StateManager::StateManager()
{
m_ActiveStateStack.clear();
}

//|||||||||||||||||||||||||||||||||||||||||||||||

StateManager::~StateManager()
{


        while(!m_ActiveStateStack.empty())
	{
		m_ActiveStateStack.back()->exit();
		delete m_ActiveStateStack.back();
		m_ActiveStateStack.pop_back();
	}

}





void StateManager::changeState(State* state)
{

	if(!m_ActiveStateStack.empty())
	{
		popState();
	}

	m_ActiveStateStack.push_back(state);
	m_ActiveStateStack.back()->enter();
}

bool StateManager::pushState(State* state)
{
	if(!m_ActiveStateStack.empty())
	{
		if(!m_ActiveStateStack.back()->pause())
			return false;
	}

	m_ActiveStateStack.push_back(state);
	m_ActiveStateStack.back()->enter();

	return true;
}

void StateManager::popState()
{
	if(!m_ActiveStateStack.empty())
	{
		m_ActiveStateStack.back()->exit();
		delete m_ActiveStateStack.back();
		m_ActiveStateStack.pop_back();
	}

	if(!m_ActiveStateStack.empty())
	{
		m_ActiveStateStack.back()->resume();
	}
  //  else

}

void StateManager::popAllAndPushState(State* state)
{
    while(!m_ActiveStateStack.empty())
    {
        m_ActiveStateStack.back()->exit();
        m_ActiveStateStack.pop_back();
    }

    pushState(state);
}

void StateManager::pauseState()
{
	if(!m_ActiveStateStack.empty())
	{
		m_ActiveStateStack.back()->pause();
	}

	if(m_ActiveStateStack.size() > 2)
	{
		//init(m_ActiveStateStack.at(m_ActiveStateStack.size() - 2));
		m_ActiveStateStack.at(m_ActiveStateStack.size() - 2)->resume();
	}
}

